<template>
  <div class="login-page">
    <div class="login-container">
      <!-- 登录表单 -->
      <div class="login-form-wrapper">
        <div class="form-header">
          <div class="logo">
            <el-icon :size="40">
              <Shop/>
            </el-icon>
            <h1>Easy Mart</h1>
          </div>
          <p class="welcome-text">欢迎登录</p>
        </div>

        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            size="large"
        >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                clearable
                placeholder="请输入用户名"
                prefix-icon="User"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                clearable
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                type="password"
                @keyup.enter="handleLogin"
            />
          </el-form-item>

          <el-form-item>
            <el-button
                :loading="loading"
                class="login-btn"
                type="primary"
                @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>

          <div class="form-footer">
            <span>还没有账号？</span>
            <el-button type="text" @click="goToRegister">立即注册</el-button>
          </div>
        </el-form>
      </div>

      <!-- 右侧装饰 -->
      <div class="login-decoration">
        <div class="decoration-content">
          <h2>购物新体验</h2>
          <p>发现更多优质商品</p>
          <p>享受便捷购物服务</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {Shop} from '@element-plus/icons-vue'
import {useCartStore, useUserStore} from '@/store'

// 路由相关
const router = useRouter()

// 状态管理
const userStore = useUserStore()
const cartStore = useCartStore()

// 响应式数据
const loading = ref(false)
const loginFormRef = ref()

// 登录表单
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, message: '密码长度不能少于6位', trigger: 'blur'}
  ]
}

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()

    loading.value = true

    // 调用登录接口
    const result = await userStore.login(loginForm)

    if (result.success) {
      ElMessage.success('登录成功')

      // 获取购物车统计
      await cartStore.fetchCartSummary()

      // 跳转到首页
      router.push('/home')
    } else {
      ElMessage.error(result.message)
    }
  } catch (error) {
    if (error !== false) {
      console.error('登录失败:', error)
      ElMessage.error('登录失败，请重试')
    }
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: var(--gradient-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.login-container {
  display: flex;
  background: var(--bg-card);
  border-radius: var(--radius-2xl);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  max-width: 900px;
  width: 100%;
  min-height: 500px;
  border: 1px solid var(--border-light);
}

/* 登录表单区域 */
.login-form-wrapper {
  flex: 1;
  padding: 60px 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
}

.logo .el-icon {
  color: var(--primary-color);
}

.logo h1 {
  margin: 0;
  font-size: 1.75rem;
  color: var(--text-primary);
  font-weight: 700;
}

.welcome-text {
  margin: 0;
  font-size: 18px;
  color: #606266;
}

.login-form {
  max-width: 350px;
  margin: 0 auto;
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 6px;
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #909399;
}

.form-footer .el-button {
  color: #409eff;
  padding: 0;
}

/* 右侧装饰区域 */
.login-decoration {
  flex: 1;
  background: linear-gradient(135deg, #409eff 0%, #36cfc9 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.login-decoration::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="2" fill="rgba(255,255,255,0.1)"/></svg>') repeat;
  animation: float 20s infinite linear;
}

@keyframes float {
  0% {
    transform: translateX(0) translateY(0);
  }
  100% {
    transform: translateX(-50px) translateY(-50px);
  }
}

.decoration-content {
  text-align: center;
  z-index: 1;
  position: relative;
}

.decoration-content h2 {
  font-size: 32px;
  margin: 0 0 20px 0;
  font-weight: 300;
}

.decoration-content p {
  font-size: 16px;
  margin: 10px 0;
  opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    margin: 10px;
  }

  .login-decoration {
    order: -1;
    min-height: 200px;
  }

  .decoration-content h2 {
    font-size: 24px;
  }

  .decoration-content p {
    font-size: 14px;
  }

  .login-form-wrapper {
    padding: 40px 30px;
  }

  .logo h1 {
    font-size: 24px;
  }

  .welcome-text {
    font-size: 16px;
  }
}

@media (max-width: 480px) {
  .login-page {
    padding: 10px;
  }

  .login-form-wrapper {
    padding: 30px 20px;
  }

  .form-header {
    margin-bottom: 30px;
  }

  .logo h1 {
    font-size: 20px;
  }

  .welcome-text {
    font-size: 14px;
  }

  .decoration-content h2 {
    font-size: 20px;
  }
}
</style>